package com.rongzhe.house.presenter;

import android.content.Intent;
import android.net.Uri;
import android.text.TextUtils;
import android.view.View;

import com.rongzhe.house.R;
import com.rongzhe.house.entity.AuthenticationInfoTo;
import com.rongzhe.house.internet.DataListener;
import com.rongzhe.house.manager.UserManager;
import com.rongzhe.house.ui.UiControlInterface;
import com.rongzhe.house.ui.activity.UserAuthActivity;
import com.rongzhe.house.utils.UIUtils;
import com.yalantis.ucrop.UCrop;

import java.io.File;
import java.util.ArrayList;

import me.iwf.photopicker.PhotoPicker;

import static android.app.Activity.RESULT_OK;

/**
 * Created by huang on 2018/1/2.
 */

public class UserAuthPresenter extends BasePresenter {
    private static final int REQUEST_IAMGE_CHOOSE_FRONT = PhotoPicker.REQUEST_CODE + 1;
    private static final int REQUEST_IAMGE_CHOOSE_BACK = PhotoPicker.REQUEST_CODE + 2;
    private static final int REQUEST_IAMGE_CROP_FRONT = UCrop.REQUEST_CROP + 1;
    private static final int REQUEST_IAMGE_CROP_BACK = UCrop.REQUEST_CROP + 2;

    public UserAuthPresenter(UiControlInterface uiControlInterface) {
        super(uiControlInterface);
    }

    private String mFrontUrl;
    private String mBackUrl;

    public void uploadFront() {
        chooseImage(true);
    }

    public void uploadBack() {
        chooseImage(false);
    }

    private void chooseImage(boolean isFront) {
        PhotoPicker.builder()
                .setPhotoCount(1)
                .setShowCamera(true)
                .setShowGif(false)
                .setPreviewEnabled(false)
                .start((UserAuthActivity) getUiControlInterface(),
                        isFront ? REQUEST_IAMGE_CHOOSE_FRONT : REQUEST_IAMGE_CHOOSE_BACK);
    }

    @Override
    public boolean handleActivityResult(int requestCode, int resultCode, Intent data) {
        if (resultCode == RESULT_OK && (requestCode == REQUEST_IAMGE_CHOOSE_FRONT
                || requestCode == REQUEST_IAMGE_CHOOSE_BACK)) {
            if (data != null) {
                ArrayList<String> photos =
                        data.getStringArrayListExtra(PhotoPicker.KEY_SELECTED_PHOTOS);
                Uri uri = Uri.fromFile(new File(photos.get(0)));
                UCrop.of(uri, uri)
                        .withAspectRatio(16, 9)
                        .withMaxResultSize(1920, 720)
                        .start((UserAuthActivity) getUiControlInterface(),
                                requestCode == REQUEST_IAMGE_CHOOSE_FRONT ?
                                        REQUEST_IAMGE_CROP_FRONT : REQUEST_IAMGE_CROP_BACK);
            }
        }
        if (resultCode == RESULT_OK && (requestCode == REQUEST_IAMGE_CROP_FRONT
                || requestCode == REQUEST_IAMGE_CROP_BACK)) {
            final Uri resultUri = UCrop.getOutput(data);
            uploadImage(resultUri, requestCode == REQUEST_IAMGE_CROP_FRONT);
        } else if (resultCode == UCrop.RESULT_ERROR) {
            final Throwable cropError = UCrop.getError(data);
        }
        return true;
    }

    private void uploadImage(Uri uri, final boolean isFront) {
        UserManager.getInstance().uploadImage(mContext, uri, new DataListener<String>() {
            @Override
            public void onSuccess(String data, String message) {
                if(isFront) {
                    mFrontUrl = data;
                    ((UserAuthActivity)getUiControlInterface()).setFrontImage(data);
                } else {
                    mBackUrl = data;
                    ((UserAuthActivity)getUiControlInterface()).setBackImage(data);
                }
            }

            @Override
            public void onFailed(Throwable error, String message) {
                getUiControlInterface().showToast(message);
            }

            @Override
            public void onEnd() {

            }
        });
    }
    private boolean checkData() {
        if (TextUtils.isEmpty(mFrontUrl)) {
            getUiControlInterface().showToast(getString(R.string.pls_upload_font_idcard));
            return false;
        } else if (TextUtils.isEmpty(mBackUrl)) {
            getUiControlInterface().showToast(getString(R.string.pls_upload_back_idcard));
            return false;
        }
        return true;
    }

    public void doAuthtication() {
        if (!checkData()) {
            return;
        }
        AuthenticationInfoTo authenticationInfoTo = new AuthenticationInfoTo(mFrontUrl, mBackUrl);
        UserManager.getInstance().uploadAuthInfo(authenticationInfoTo, new DataListener<Object>() {
            @Override
            public void onSuccess(Object data, String message) {
                getUiControlInterface().showAlert(UIUtils.createHintMessageWithClick(
                        mContext, getString(R.string.hint),
                        getString(R.string.authinfo_upload_success), getString(android.R.string.ok),
                        new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                getUiControlInterface().finish();
                            }
                        }));
            }

            @Override
            public void onFailed(Throwable error, String message) {
                getUiControlInterface().showToast(message);
            }

            @Override
            public void onEnd() {
            }
        });
    }

}
